/**
 * @author Mahesh
 */
var MICRO_BREAK_FREQUENCY_MINUTES = 10;
var MICRO_BREAK_DURATION_MINUTES = 0.5;
var MICRO_BREAK_NOTIFICATION_AUDIO_FILE = "../Resources/alert.wav";

var MACRO_BREAK_FREQUENCY_MINUTES = 60;
var MACRO_BREAK_DURATION_MINUTES = 10;
var MACRO_BREAK_NOTIFICATION_AUDIO_FILE = "../Resources/alert.wav";

var notification = null;
var breakSkipped = false;

var showNotificationInterval;
var closeNotificationInterval;

var nextBreakTime;

window.addEventListener('load', onloadFunction, false);
function onloadFunction()
{
	// Event handlers
	document.getElementById("pauseResumeBreak").onclick = pauseBreak;
	document.getElementById("enableNotifications").onclick = enableDesktopNotifications;
	
	// To be deleted in final release, once the settings logic is complete
	// localStorage.clear();
	
	// Store default settings in the local storage
	// This can be moved into the respective reminder objects constructors
	loadDefaults();
	
	startTimer();
} 

function startTimer()
{
	showNotificationInterval = self.setInterval(showReminder, localStorage.getItem('micro_break_frequency_minutes')*60*1000);

	// Calcualte when the next break is scheduled to begin to display in main window
	nextBreakTime = getNextBreakTime().toLocaleTimeString();
	document.getElementById('nextBreakTime').innerHTML = nextBreakTime; 
}

function showReminder()
{
	clearInterval(showNotificationInterval);
	
	if (window.webkitNotifications.checkPermission() == 0)
	{	
		// Calculate the next break just before showing the notification and persist in local storage. 
		// This value + break duration will give the exact time when the next break will begin. 
		nextBreakTime = getNextBreakTime().toString();
		document.getElementById('nextBreakTime').innerHTML = "Break Running";
		localStorage.setItem('nextBreakTime', nextBreakTime);
		
		// Chaining the show() call below will not work, god knows why
		notification = window.webkitNotifications.createHTMLNotification("Pages/notification.html");
		notification.onclose = restartTimer;
		if(parseBoolean(localStorage.getItem('playSound')) == true)
		{
			playSound();	// Play the notification sound
		}
		notification.show();
	}
	else
	{
		alert("Please Enable Desktop Notifications");
	}
	
	closeNotificationInterval = setInterval(function(){breakComplete(notification);}, localStorage.getItem('micro_break_duration_minutes')*60*1000);
}

//Calculate when next break begins
function getNextBreakTime()
{
	var currentTime = new Date();
	var nextBreakTime = new Date();

	nextBreakTime.setTime(currentTime.getTime() + localStorage.getItem('micro_break_frequency_minutes')*60*1000);
	return nextBreakTime;
}

function breakComplete(notification)
{
	if(parseBoolean(localStorage.getItem('playSound')) == true)
	{
		playSound();	// Play the notification sound
	}
	notification.cancel();
	restartTimer();
}

function restartTimer()
{
	clearInterval(showNotificationInterval);
	clearInterval(closeNotificationInterval);
	startTimer();
}

function resumeBreak()
{
	// Change the button to handle pause request
	document.getElementById("pauseResumeBreak").onclick = pauseBreak;
	document.getElementById("pauseResumeBreak").innerHTML = "Pause Breaks";
	
	restartTimer();
}

function pauseBreak()
{
	// Change the button to handle resume request
	document.getElementById("pauseResumeBreak").onclick = resumeBreak;
	document.getElementById("pauseResumeBreak").innerHTML = "Resume Breaks";
	
	document.getElementById('nextBreakTime').innerHTML = "No breaks scheduled";

	if(notification != null) {
		// Remove the associated events on the notification
		notification.onclose = null;
		notification.cancel();
	}
	clearInterval(showNotificationInterval);
	clearInterval(closeNotificationInterval);
}

function enableDesktopNotifications()
{
	if (window.webkitNotifications.checkPermission() != 0)
	{	    
		window.webkitNotifications.requestPermission();
	}
}

function playSound() {
	// Play the sound on a different thread
	setTimeout(function() {
		var alertSound = new Audio(MICRO_BREAK_NOTIFICATION_AUDIO_FILE);
		alertSound.play();
	}, 50);
}